package com.dbq.fast.business.me.adapter;

import android.annotation.SuppressLint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.business.me.bean.RewardInfo;
import com.dbq.fast.business.me.viewholder.DividerViewHolder;
import com.dbq.fast.business.me.viewholder.FooterViewHolder;
import com.dbq.fast.business.me.viewholder.RewardItemViewHolder;
import com.dbq.fast.util.DateUtil;

import java.util.List;

/**
 * Created by yuanchen on 2018/5/4.
 */

public class RewardAdapter extends RecyclerView.Adapter {

    public static final int TYPE_ITEM = 0;
    public static final int TYPE_FOOTER = 1;
    public static final int TYPE_DIVIDER = 2;

    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE = 1;
    //没有加载更多
    public static final int NO_LOAD_MORE = 2;
    //上拉加载更多状态-默认为0
    private int mLoadMoreStatus = 0;

    private List<RewardInfo.AwardListBean> rewardInfos;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_reward, parent, false);
            return new RewardItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View footerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_reward, parent, false);
            return new FooterViewHolder(footerView);
        } else {
            View dividerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_divider, parent, false);
            return new DividerViewHolder(dividerView);
        }
    }

    @SuppressLint("StringFormatInvalid")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RewardItemViewHolder) {
            RewardItemViewHolder itemVH = (RewardItemViewHolder) holder;
            RewardInfo.AwardListBean rewardInfo = rewardInfos.get(position);
            if (rewardInfo != null) {
                itemVH.rewardId.setText(App.getContext().getString(R.string.reward_id, rewardInfo.getInviteeVpnId()));
                itemVH.payTime.setText(App.getContext().getString(R.string.reward_pay_time, DateUtil.transFormat(rewardInfo.getCreate())));
                itemVH.payMoney.setText(App.getContext().getString(R.string.reward_pay_money, "¥"+rewardInfo.getPrice()));
                itemVH.rewardMoney.setText("+¥"+rewardInfo.getAward());
                String level = App.getContext().getString(R.string.level_1);
                if(rewardInfo.getLevel() == 2){
                    level = App.getContext().getString(R.string.level_2);
                }else if(rewardInfo.getLevel() == 3){
                    level = App.getContext().getString(R.string.level_3);
                }
                itemVH.level.setText(level);
            }
        } else if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerVH = (FooterViewHolder) holder;
            switch (mLoadMoreStatus) {
                case PULLUP_LOAD_MORE:
                    footerVH.pbLoad.setVisibility(View.GONE);
                    footerVH.footerTip.setText(App.getContext().getString(R.string.pull_up_to_load_more));
                    break;
                case LOADING_MORE:
                    footerVH.pbLoad.setVisibility(View.VISIBLE);
                    footerVH.footerTip.setText(App.getContext().getString(R.string.loading_more));
                    break;
                case NO_LOAD_MORE:
                    footerVH.pbLoad.setVisibility(View.GONE);
                    footerVH.footerTip.setText(App.getContext().getString(R.string.no_more));
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return rewardInfos == null ? 0 : rewardInfos.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else if (position == 0) {
            return TYPE_DIVIDER;
        } else {
            return TYPE_ITEM;
        }
    }

    public void setData(List<RewardInfo.AwardListBean> rewardInfos) {
        this.rewardInfos = rewardInfos;
        this.rewardInfos.add(0, null);
        notifyDataSetChanged();
    }

    public void addFooterData(List<RewardInfo.AwardListBean> rewardInfos) {
        if (this.rewardInfos == null || rewardInfos == null) return;
        this.rewardInfos.addAll(rewardInfos);
        notifyDataSetChanged();
    }

    public void changeMoreStatus(int status) {
        mLoadMoreStatus = status;
        notifyDataSetChanged();
    }

}
